public class DefEntCrossTrigger {
    public static boolean InEntUpdateTrigger = false;
    
    public static Set<Id> DirtySPs = new Set<Id>();
    public static void FixSPDefEntHints(Set<Id> idArr) {
        List<Support_Program__c> updateSPArr = new List<Support_Program__c>();
        for (Support_Program__c sp : [Select DefEntHint__c,(Select Id From Entitlements__r Where IsDefault__c=True And EntState__c='Active') From Support_Program__c Where Id In :idArr]) {
            sp.DefEntHint__c = (sp.Entitlements__r.size()==0)?(null):(sp.Entitlements__r[0].Id);
            updateSPArr.add(sp);
        }
        SP_Trigger.DefEntHintUnlocked = true;
        update updateSPArr;
        SP_Trigger.DefEntHintUnlocked = false;
    }
    
    static testMethod void testEntTriggers() {
        UT_SObjectFactory.InterconnectedSObjectsForMASP o = new UT_SObjectFactory.InterconnectedSObjectsForMASP(1);
        Entitlement__c e1 = o.addDefEnt(); // does an insert
        System.assertEquals(true,[Select IsDefault__c From Entitlement__c Where Id=:e1.Id].IsDefault__c,'Insert of a def ent should work.');
        System.assertEquals(e1.Id,[Select DefEntHint__c From Support_Program__c Where Id=:o.sp.Id].DefEntHint__c,'Parent should reference child def ent');       
        
        Entitlement__c e2 = o.addDefEnt(); // does an insert
        System.assertEquals(false,[Select IsDefault__c From Entitlement__c Where Id=:e1.Id].IsDefault__c,'Second insert should have undefaulted this.');
        System.assertEquals(true,[Select IsDefault__c From Entitlement__c Where Id=:e2.Id].IsDefault__c,'Most recently inserted def ent should stay default.');
        System.assertEquals(e2.Id,[Select DefEntHint__c From Support_Program__c Where Id=:o.sp.Id].DefEntHint__c,'Parent should reference child def ent');       
                
        e1.IsDefault__c = true;
        update e1;
        System.assertEquals(true,[Select IsDefault__c From Entitlement__c Where Id=:e1.Id].IsDefault__c,'Update should have defaulted e1');
        System.assertEquals(false,[Select IsDefault__c From Entitlement__c Where Id=:e2.Id].IsDefault__c,'Update should have undefaulted e2');       
        System.assertEquals(e1.Id,[Select DefEntHint__c From Support_Program__c Where Id=:o.sp.Id].DefEntHint__c,'Parent should reference child def ent');       
            
        e1.IsDefault__c = false;
        update e1;
        System.assertEquals(0,[Select Count() From Entitlement__c Where Id In (:e1.Id,:e2.Id) And IsDefault__c=True],'Update should have left no default ent');
        System.assertEquals(null,[Select DefEntHint__c From Support_Program__c Where Id=:o.sp.Id].DefEntHint__c,'Parent has no child def ent');       
    
        e1.IsDefault__c = true;
        e2.IsDefault__c = true;
        Entitlement__c[] eArr = new Entitlement__c[]{e1,e2};
        update eArr;
        System.assertEquals(1,[Select Count() From Entitlement__c Where Id In (:e1.Id,:e2.Id) And IsDefault__c=True],'Update should pick one or the other as default ent');        
        Id eNewDEId = [Select Id From Entitlement__c Where Id In (:e1.Id,:e2.Id) And IsDefault__c=True].Id;
        System.assertEquals(eNewDEId,[Select DefEntHint__r.Id From Support_Program__c Where Id=:o.sp.Id].DefEntHint__r.Id,'Parent has no child def ent');       
   
        delete new Entitlement__c(Id=eNewDEId);
        System.assertEquals(0,[Select Count() From Entitlement__c Where Id In (:e1.Id,:e2.Id) And IsDefault__c=True],'Delete should have left no default ent');
        System.assertEquals(null,[Select DefEntHint__c From Support_Program__c Where Id=:o.sp.Id].DefEntHint__c,'Parent has no child def ent');       
                
    }
}